package Javatest;

public class test_6_6 {


    public static class Person {
        private String name;
        private int age;
        private String address;

        public Person(String name, int age, String address) {
            this.name = name;
            this.age = age;
            this.address = address;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public String getAddress() {
            return address;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public void displayInfo() {
            System.out.println("Name: " + name + ", Age: " + age + ", Address: " + address);
        }
    }


    public static class Student extends Person {
        private int studentId;
        private String major;
        private double gpa;

        public Student(String name, int age, String address, int studentId, String major, double gpa) {
            super(name, age, address);
            this.studentId = studentId;
            this.major = major;
            this.gpa = gpa;
        }

        public int getStudentId() {
            return studentId;
        }

        public void setStudentId(int studentId) {
            this.studentId = studentId;
        }

        public String getMajor() {
            return major;
        }

        public void setMajor(String major) {
            this.major = major;
        }

        public double getGpa() {
            return gpa;
        }

        public void setGpa(double gpa) {
            this.gpa = gpa;
        }

        @Override
        public void displayInfo() {
            super.displayInfo();
            System.out.println("Student ID: " + studentId + ", Major: " + major + ", GPA: " + gpa);
        }

        public boolean isHonorStudent() {
            return gpa >= 3.5;
        }
    }


    public static void main(String[] args) {
        Person person = new Person("John", 30, "123 Main St");
        person.displayInfo();

        Student student = new Student("Alice", 22, "456 Oak St", 101, "Computer Science", 3.8);
        student.displayInfo();
        System.out.println("Is honor student? " + student.isHonorStudent());

        Person person2 = new Student("Bob", 25, "789 Pine St", 102, "Math", 3.2);
        person2.displayInfo();



        //instanceof 是 Java 中的一个关键字，主要用于在运行时检查一个对象是否是特定类型（类或子类）的实例。
        //它的作用是进行类型检查。这里通过 person2 instanceof Student 来判断 person2 对象实际上是不是 Student 类的实例。
        //如果是，就可以进行相应的强制类型转换并使用子类特有的方法和属性。
        if (person2 instanceof Student) {
            Student student2 = (Student) person2;
            System.out.println(student2.getStudentId());
            System.out.println(student2.getMajor());
            System.out.println(student2.getGpa());
        }
    }

}
